package com.pn.serviceimpl;

import com.pn.entity.Pet;
import com.pn.mapper.PetMapper;
import com.pn.service.PetService;
import com.pn.util.PageCriterion;
import com.pn.util.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PetServiceImpl implements PetService {

    @Autowired
    PetMapper petMapper;

    @Override
    public List<Pet> listByPage(PageCriterion pageCriterion, Integer sellStatus) {
        List<Pet> pets;
        System.out.println("PetService正在为您查询~");
        switch (sellStatus){
            case 0:
                return pets = petMapper.listAllByCriterion(pageCriterion);
            case 1:
                return pets = petMapper.listAvaliableByCriterion(pageCriterion);
            case 2:
                return pets = petMapper.listAdoptedByCriterion(pageCriterion);
            default:
                return null;
        }
    }

    @Override
    public List<Pet> listByUidByPage(PageCriterion pageCriterion, Integer uid) {
        System.out.println("PetService正在为您查询~");
        return petMapper.listByUidByCriterion(pageCriterion.getStart(),pageCriterion.getPageSize(),uid);
    }

    @Override
    public List<Pet> listFuzzyByPage(PageCriterion pageCriterion, String searchKey) {
        return null;
    }

    @Override
    public ResponseMsg insert(Pet pet) {
        System.out.println("Service正在为您插入数据...");
        ResponseMsg responseMsg = new ResponseMsg();
        int insertLine = petMapper.insertSelective(pet);
        if(insertLine == 1){
            responseMsg.setRespStatusCode(200);
            responseMsg.setRespMsg("插入成功！");
        } else {
            responseMsg.setRespStatusCode(300);
            responseMsg.setRespMsg("插入失败");
        }
        return responseMsg;
    }

    @Override
    public Pet getById(Integer pid) {
        System.out.println("Service正在为您查询~");
        return petMapper.selectByPrimaryKey(pid);
    }

    @Override
    public List<Pet> listByPtidByPage(PageCriterion pageCriterion, Integer ptid) {
        System.out.println("Service正在为您查询~");
        if(ptid == -1){
            return petMapper.listAllByCriterion(pageCriterion);
        } else{
            return petMapper.listByPtidByCriterion(pageCriterion.getStart(),pageCriterion.getPageSize(),ptid);
        }
    }
}
